﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core;
using System.Text;

namespace Web.Controllers
{
    public class userController : JsonController
    {
        //
        // GET: /user/

        public ActionResult getCurrentUserInfo()
        {
            var u = Certify.user;
            if (u == null) return Json(new { success = false });
            return Json(u);
        }

        [NeedLogon]
        public ActionResult doLogout()
        {
            Certify.user = null;
            Certify.regsid = null;
            return Json(new { success = true });
        }

        [ParamsNoEmpty]
        public ActionResult doLogin(string username, string password)
        {
            var result = user.doLogin(username, password);
            if (result.success)
            {
                if (result.student == null)
                {
                    var u = user.getUser(result.username);
                    Certify.user = new Certify.User
                    {
                        username = u.username,
                        nickname = u.nickname,
                        email = u.email,
                        motto = u.motto,
                        sid = u.sid,
                        name = u.name,
                        school = u.school,
                        sex = u.sex,
                        birthday = u.birthday,
                        publicPersonalInfo = u.publicPersonalInfo,
                        admin = u.admin,
                        teacher = u.teacher,
                        god = false
                    };
                }
                else
                    Certify.regsid = result.student.sid;
            }
            return Json(result);
        }

        [NeedOpenReg]
        public ActionResult doReg(string username, string password, string nickname, string email, string motto,
            string sid, string name, string school, int sex, string birthday, string publicPersonalInfo)
        {
            CheckEmpty(username, password, email, school);
            var _birthday = CheckDateQ(birthday);
            sid = sid.ToQ();
            if (sid != null && sid != Certify.regsid) return Json(new
                  {
                      success = false,
                      msg = "学号验证错误"
                  });

            int len = Encoding.GetEncoding("gb2312").GetByteCount(username);
            if (len < 4 || len > 20) return Json(new
            {
                success = false,
                msg = "用户名长度不正确，必须为4-20个字节"
            });

            if (!Certify.god)
            {
                var testName = username.ToLower();
                var disInNames = new string[] { "admin", "teacher", "管理", "系统" };
                foreach (string disName in disInNames)
                    if (testName.Contains(disName)) goto nopass;
                var disIsNames = new string[] { "user", "username", "test", "hfut", "onlinejudge", "system", "manager", "master", "root", "icpc", "acmicpc" };
                foreach (string disName in disIsNames)
                    if (testName == disName) goto nopass;
                goto pass;
            nopass:
                return Json(new
                {
                    success = false,
                    msg = "该用户名被禁止注册，请使用其他用户名"
                });
            pass: ;
            }

            var result = user.doReg(username, password, nickname.ToQ(), email, motto.ToQ(),
                sid, name.ToQ(), school.ToQ(), sex.ToSex(), _birthday, publicPersonalInfo.ToBool());
            if (result.success && !Certify.god)
            {
                Certify.regsid = null;
                var u = user.getUser(username);
                Certify.user = new Certify.User
                {
                    username = u.username,
                    nickname = u.nickname,
                    email = u.email,
                    motto = u.motto,
                    sid = u.sid,
                    name = u.name,
                    school = u.school,
                    sex = u.sex,
                    birthday = u.birthday,
                    publicPersonalInfo = u.publicPersonalInfo,
                    admin = u.admin,
                    god = false
                };
            }
            return Json(result);
        }

        public ActionResult getUserInfo(string username)
        {
            return Json(user.getUserInfo(username, Certify.username, Certify.god));
        }

        [Myself]
        public ActionResult editUserInfo(string username, string nickname, string email, string motto,
            string publicPersonalInfo, string name, string school, int sex, string birthday)
        {
            CheckEmpty(username, email, school);
            var _birthday = CheckDateQ(birthday);
            return Json(user.editUserInfo(username, nickname.ToQ(), email, motto.ToQ(),
                publicPersonalInfo.ToBool(), name.ToQ(), school.ToQ(), sex.ToSex(), _birthday));
        }

        [ParamsNoEmpty]
        public ActionResult getUserCompare(string usernameA, string usernameB)
        {
            return Json(user.getUserCompare(usernameA, usernameB, Certify.username));
        }

        [ParamsNoEmpty]
        public ActionResult getUserStatistics(string username)
        {
            return Json(user.getUserStatistics(username, Certify.username));
        }

        [Myself]
        public ActionResult editUserPassword(string username, string oldPassword, string password)
        {
            CheckEmpty(username, password);
            return Json(user.editUserPassword(username, oldPassword, password, Certify.god));
        }

        public ActionResult getRanklist(int start, int limit)
        {
            return Json(user.getRanklist(start, limit));
        }

        public ActionResult getUserRank(string username)
        {
            return Json(user.getUserRank(username));
        }

        [OnlyGod]
        public ActionResult getUserList(int start, int limit)
        {
            return Json(user.getUserList(start, limit));
        }

        [OnlyGod]
        [ParamsNoEmpty]
        public ActionResult deleteUser(string username)
        {
            return Json(user.deleteUser(username));
        }

        [OnlyGod]
        [ParamsNoEmpty]
        public ActionResult setUserDisabled(string username, bool disabled)
        {
            return Json(user.setUserDisabled(username, disabled));
        }

        [OnlyGod]
        [ParamsNoEmpty]
        public ActionResult setUserAdmin(string username, string myPassword, bool admin)
        {
            if (!user.checkPassword(Certify.username, myPassword)) return Json(new
            {
                success = false,
                msg = "密码验证失败"
            });
            return Json(user.setUserAdmin(username, admin));
        }

        [ParamsNoEmpty]
        public ActionResult recoveryUserPassword(string username, string sid, string remotePassword, string password)
        {
            return Json(user.recoveryUserPassword(username, sid, remotePassword, password));
        }
    }
}
